package com.bw.lib_base.base

import android.util.Log
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.SPUtils
import com.google.gson.Gson
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.lang.Exception
import java.lang.RuntimeException
import java.text.SimpleDateFormat

open class BaseViewModel:ViewModel(), DefaultLifecycleObserver {
    // 生命周期持有变量（延迟初始化）
    protected lateinit var lifecycle: Lifecycle

    // 初始化
    override fun onStart(owner: LifecycleOwner) {
        super.onStart(owner)
        lifecycle = owner.lifecycle// 绑定生命周期
    }

    protected fun isInitLife():Boolean{
        //使用属性引用
        return ::lifecycle.isInitialized// 检查是否完成初始化
    }

    override fun onCleared() {
        super.onCleared()
        if (::lifecycle.isInitialized){
            lifecycle.removeObserver(this)//防止内存泄露
        }
    }


}